package com.jaynux.app;
import android.content.*;
import android.content.res.*;
import android.graphics.drawable.*;
import android.os.*;

public class Horses{
	public Specification []Horse = new Specification[5];
	int NUMBER = 1;
	Resources res;
	enum  horse_num{관우, 여포, 조운, 하후돈, 태희, 마초, 유비, 장비,황충};
	
	int imgHorse[][] = {{R.drawable.g_h1,R.drawable.g_h2, R.drawable.g_at1, R.drawable.g_at2,
		R.drawable.g_at3, R.drawable.g_at3, R.drawable.g_b1, R.drawable.g_b2},
		{R.drawable.u_h1,R.drawable.u_h2, R.drawable.u_at1, R.drawable.u_at2,
		R.drawable.u_at3, R.drawable.u_at4, R.drawable.u_b1, R.drawable.u_b2},
		{R.drawable.j_h1,R.drawable.j_h2, R.drawable.j_at1, R.drawable.j_at2,
		R.drawable.j_at3, R.drawable.j_at4, R.drawable.j_b1, R.drawable.j_b2},
		{R.drawable.h_h1,R.drawable.h_h2, R.drawable.h_at1, R.drawable.h_at2,
		R.drawable.h_at3, R.drawable.h_at4, R.drawable.h_b1, R.drawable.h_b2},
		{R.drawable.k_h1,R.drawable.k_h2, R.drawable.k_at1, R.drawable.k_at2,
		R.drawable.k_at3, R.drawable.k_at4, R.drawable.k_b1, R.drawable.k_b2},
		{R.drawable.ma_cho_h1,R.drawable.ma_cho_h2, R.drawable.ma_cho_at1, R.drawable.ma_cho_at2,
		R.drawable.ma_cho_at3, R.drawable.ma_cho_at4, R.drawable.ma_cho_b1, R.drawable.ma_cho_b2},
		{R.drawable.yu_bi_h1,R.drawable.yu_bi_h2, R.drawable.yu_bi_at1, R.drawable.yu_bi_at2,
		R.drawable.yu_bi_at3, R.drawable.yu_bi_at4, R.drawable.yu_bi_b1, R.drawable.yu_bi_b2},
		{R.drawable.jang_bi_h1,R.drawable.jang_bi_h2, R.drawable.jang_bi_at1, R.drawable.jang_bi_at2,
		R.drawable.jang_bi_at3, R.drawable.jang_bi_at4, R.drawable.jang_bi_b1, R.drawable.jang_bi_b2},
		{R.drawable.hwang_chung_h1,R.drawable.hwang_chung_h2, R.drawable.hwang_chung_at1, R.drawable.hwang_chung_at2,
		R.drawable.hwang_chung_at3, R.drawable.hwang_chung_at4, R.drawable.hwang_chung_b1, R.drawable.hwang_chung_b2}
		};
	
	class Specification extends Thread{
		int horseNum;
		public Specification(int arAg, int arSpeed, int arBooster, int arDamage, int arBoosterChance, String arName) {
			super();
			SPEED = arSpeed;
			AG = arAg;
			DAMAGE = arDamage;
			mBoosterSpeed = arBooster;
			mBoosterChance = arBoosterChance;
			NAME = arName;
			LOCATION = 0;
			FRAME = 0;
			preStep = 0;
			BGROUND_REFLASH = 1;
			setDrawble();
			stopwatch = 0;
			RANK = 0;
		}
		
		public void setDrawble() {
			horseNum = horse_num.valueOf(NAME).ordinal();
			mHImage[0] = res.getDrawable(imgHorse[horseNum][0]);
			mHImage[1] = res.getDrawable(imgHorse[horseNum][1]);
			mHImage[2] = res.getDrawable(imgHorse[horseNum][0]);
			mHImage[3] = res.getDrawable(imgHorse[horseNum][1]);
			
			
		}
		
		public void setDieDrawble() {	
			mObstacleImage[0] = res.getDrawable(R.drawable.dummy_at1);
			mObstacleImage[1] = res.getDrawable(R.drawable.dummy_at2);
			mObstacleImage[2] = res.getDrawable(R.drawable.dummy_at3);
			mObstacleImage[3] = res.getDrawable(R.drawable.dummy_at4);
		}
		
		public void setShowDrawble() {
			mHImage[0] = res.getDrawable(imgHorse[horseNum][2]);
			mHImage[1] = res.getDrawable(imgHorse[horseNum][3]);
			mHImage[2] = res.getDrawable(imgHorse[horseNum][4]);
			mHImage[3] = res.getDrawable(imgHorse[horseNum][5]);
			
			mObstacleImage[0] = res.getDrawable(R.drawable.dummy_at1);
			mObstacleImage[1] = res.getDrawable(R.drawable.dummy_at2);
			mObstacleImage[2] = res.getDrawable(R.drawable.dummy_at3);
			mObstacleImage[3] = res.getDrawable(R.drawable.dummy_at4);
		}
		
		public void setStopWatch(int arTime) {
			stopwatch = arTime;
		}
		
		public int getStopWatch()
		{
			return stopwatch;
		}
		
		@Override
		public void run(){
			while(true){
				while(mPause) {
					try {
						if(cnt == 1){
							try {
								sleep(1000);
							} catch (InterruptedException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							cnt = 0;
						}

						if((int)(Math.random() * mBoosterChance*10) + 1 == 1) {
							obstableFlag = true;
							FRAME = 0;
							while(obstableFlag) {
								FRAME++;
								mCurrentSpeed = 0;
								LOCATION += mCurrentSpeed;
								sleep(100);
							}

						}
						//부스터가 확률 적으로 나가게 한다.
						if( (int)(Math.random() * mBoosterChance*10) + 1 == mBoosterChance ){
							//부스터 터질떄 칼질을 한번만 하기위해 초기 프레임을 저장 한다.
							FRAME = 0;
							int preventFrame = FRAME;
							//부스터가 한번 터지면 일정한 거리만큼을 보장하여 준다.
							//location 1000만큼은 부스터 상태로 이동 할 수 있다. 한번 터질경우
							for(int preventLocation = LOCATION; LOCATION < preventLocation+1000; ){
								// 이동중 모션을 업그레이드 하기 위한 FRAME 알고리즘 이다.
								FRAME++;
								if(FRAME<=preventFrame+5){
									//부스터 발동 거는 모습
									mHImage[0] = res.getDrawable(imgHorse[horseNum][2]);
									mHImage[1] = res.getDrawable(imgHorse[horseNum][3]);
									mHImage[2] = res.getDrawable(imgHorse[horseNum][4]);
									mHImage[3] = res.getDrawable(imgHorse[horseNum][5]);
								}
								else{
									//부스터 발동 후 모션으로 변경하여준다.
									mHImage[0] = res.getDrawable(imgHorse[horseNum][6]);
									mHImage[1] = res.getDrawable(imgHorse[horseNum][7]);
									mHImage[2] = res.getDrawable(imgHorse[horseNum][6]);
									mHImage[3] = res.getDrawable(imgHorse[horseNum][7]);
								}
								//속력을 구해야 배경화면을 움직이는 기준을 변경 할 수 있다.
								mCurrentSpeed = (int)(Math.random()*SPEED)+mBoosterSpeed; // 순간적으로 이동하는 속도
								//현재속력을 위치에 적용하여 준다.
								LOCATION += mCurrentSpeed;

								// 확률적으로 현재속력에 의해서 말의 위치가 변경되는것을 바로 할지 안할지를 결정한다.
								// 즉 틱을 의미한다.
								sleep((int)(Math.random()*AG+20));
								if(LOCATION > meter) 
									break;

							}
						}else{
							// 이동중 모션을 업그레이드 하기 위한 FRAME 알고리즘 이다.
							FRAME++;
							mHImage[0] = res.getDrawable(imgHorse[horseNum][0]);
							mHImage[1] = res.getDrawable(imgHorse[horseNum][1]);
							mHImage[2] = res.getDrawable(imgHorse[horseNum][0]);
							mHImage[3] = res.getDrawable(imgHorse[horseNum][1]);
							//속력을 구해야 배경화면을 움직이는 기준을 변경 할 수 있다.
							mCurrentSpeed = (int)(Math.random()*SPEED); // 순간적으로 이동하는 속도
							LOCATION += mCurrentSpeed;
							sleep((int)(Math.random()*AG+20)); // 틱의 빈도
						}

						preStep = LOCATION;

						if(LOCATION >= meter){
							mHImage[0] = res.getDrawable(imgHorse[horseNum][0]);
							mHImage[1] = res.getDrawable(imgHorse[horseNum][0]);
							mHImage[2] = res.getDrawable(imgHorse[horseNum][0]);
							mHImage[3] = res.getDrawable(imgHorse[horseNum][0]);
							RANK = NUMBER;
							NUMBER++;
							break;
						}

					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				}
				try {
					sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(RANK !=0)
					break;
			}

		}
		int cnt = 1;
		int preStep;
		int FRAME;
		int AG;
		int SPEED;
		int DAMAGE;
		int mCurrentSpeed;
		int mBoosterSpeed;
		int mBoosterChance;
		int LOCATION;
		int RANK = 0;
		int BGROUND_REFLASH;
		int meter;
		int stopwatch;
		boolean obstableFlag = false;
		boolean mPause = true;
		String playerName;
		String NAME;
		Drawable mHImage[] = new Drawable[4];	
		Drawable mObstacleImage[] = new Drawable[4];	
	}

	Horses(Context context){
		res = context.getResources();
		/*
		 * param 1 : AG 가 틱을 의미한다. (보폭)
		 * param 2 : speed 가 평속을 의미한다. 
		 * param 3 : 부스터
		 */

		Horse[0] = new Specification(10,20, 5, 5, 10, "태희");
		Horse[1] = new Specification(10,20, 5, 5, 10, "태희");
		Horse[2] = new Specification(10,20, 5, 5, 10, "태희");
		Horse[3] = new Specification(10,20, 5, 5, 10, "태희");
	}

	public void setSettingValue(Bundle b) {
		// TODO Auto-generated method stub

		Horse[0].playerName = b.getString("player1Name");
		Horse[0].AG = b.getInt("player1_ag");
		Horse[0].SPEED = b.getInt("player1_speed");
		Horse[0].DAMAGE = b.getInt("player1_damage");
		Horse[0].mBoosterSpeed = b.getInt("player1_booster");
		Horse[0].NAME = b.getString("player1_hero");
		Horse[0].meter = b.getInt("meter");
		Horse[0].setDrawble();


		
		Horse[1].playerName = b.getString("player2Name");
		Horse[1].AG = b.getInt("player2_ag");
		Horse[1].SPEED = b.getInt("player2_speed");
		Horse[1].DAMAGE = b.getInt("player2_damage");
		Horse[1].mBoosterSpeed = b.getInt("player2_booster");
		Horse[1].NAME = b.getString("player2_hero");
		Horse[1].meter = b.getInt("meter");
		Horse[1].setDrawble();
		
	
		Horse[2].playerName = b.getString("player3Name");	
		Horse[2].AG = b.getInt("player3_ag");
		Horse[2].SPEED = b.getInt("player3_speed");
		Horse[2].DAMAGE = b.getInt("player3_damage");
		Horse[2].mBoosterSpeed = b.getInt("player3_booster");
		Horse[2].NAME = b.getString("player3_hero");
		Horse[2].meter = b.getInt("meter");
		Horse[2].setDrawble();
		
		
		Horse[3].playerName = b.getString("player4Name");
		Horse[3].AG = b.getInt("player4_ag");
		Horse[3].SPEED = b.getInt("player4_speed");
		Horse[3].DAMAGE = b.getInt("player4_damage");
		Horse[3].mBoosterSpeed = b.getInt("player4_booster");
		Horse[3].NAME = b.getString("player4_hero");
		Horse[3].meter = b.getInt("meter");
		Horse[3].setDrawble();
	}
	
}
